package com.chexiehui.jc.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.chexiehui.jc.R;
import com.chexiehui.jc.entity.BaiDuLocationInfo;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.ShopInfo;
import com.chexiehui.jc.utils.AppCommonUtil;
import com.chexiehui.jc.utils.GlideUtil;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.KeyboardUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CustomStageBuyCarPopup extends BottomPopupView {

    @BindView(R.id.tv_popup_title)
    TextView tvPopupTitle;
    @BindView(R.id.iv_popup_close)
    ImageView ivPopupClose;
    @BindView(R.id.iv_car_image)
    ImageView ivCarImage;
    @BindView(R.id.tv_car_name)
    TextView tvCarName;
    @BindView(R.id.tv_choose_car_model)
    TextView tvChooseCarModel;
    @BindView(R.id.rl_choose_model)
    RelativeLayout rlChooseModel;
    @BindView(R.id.et_person_phone)
    EditText etPersonPhone;
    @BindView(R.id.et_person_name)
    EditText etPersonName;
    @BindView(R.id.tv_person_city)
    TextView tvPersonCity;
    @BindView(R.id.ll_choose_city)
    LinearLayout llChooseCity;
    @BindView(R.id.tv_car_distributor)
    TextView tvCarDistributor;
    @BindView(R.id.ll_choose_merchant)
    LinearLayout llChooseMerchant;
    @BindView(R.id.rtv_apply)
    RadiusTextView rtvApply;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.rl_choose_model_no)
    RelativeLayout rlChooseModelNo;
    private Unbinder mUnBinder;
    private CarInfo chooseModel;
    private BaiDuLocationInfo chooseCity;
    private List<String> chooseMerchantIds;
    private ICustomStageBuyCarPopupListener listener;

    public CustomStageBuyCarPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_custom_stage_buy_car_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
    }

    public void initCarModel(CarInfo carInfo){
        this.chooseModel = carInfo;
    }
    public void initCity(BaiDuLocationInfo city){
        this.chooseCity = city;
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
        setModelInfo();
        setCityInfo();
        setMerchantInfo();
    }

    public void setListener(ICustomStageBuyCarPopupListener listener) {
        this.listener = listener;
    }

    private void setModelInfo(){
        if(chooseModel == null){
            rlChooseModelNo.setVisibility(VISIBLE);
            rlChooseModel.setVisibility(GONE);
        }else{
            rlChooseModelNo.setVisibility(GONE);
            rlChooseModel.setVisibility(VISIBLE);
            GlideUtil.loadImageViewCrop(getContext(), chooseModel.getImg(), ivCarImage, R.drawable.shape_square, R.drawable.shape_square);
            tvCarName.setText(chooseModel.getName());
        }
    }

    private void setCityInfo(){
        if(chooseCity == null){
            tvPersonCity.setText("");
        }else{
            tvPersonCity.setText(chooseCity.getCity());
        }
    }

    @SuppressLint({"SetTextI18n", "DefaultLocale"})
    private void setMerchantInfo(){
        if(chooseMerchantIds == null || chooseMerchantIds.size() < 1){
            tvCarDistributor.setText("");
        }else{
            tvCarDistributor.setText(String.format("已选择%d家经销商", chooseMerchantIds.size()));
        }
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
        KeyboardUtils.hideSoftInput(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    @OnClick({R.id.iv_popup_close, R.id.rl_choose_model, R.id.rl_choose_model_no, R.id.ll_choose_city, R.id.ll_choose_merchant, R.id.rtv_apply, R.id.tv_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_popup_close:
                dismiss();
                break;
            case R.id.rl_choose_model_no:
            case R.id.rl_choose_model:
                if(listener != null){
                    listener.onStageBuyCarPopupCallChooseModel();
                }
                break;
            case R.id.ll_choose_city:
                if(listener != null){
                    listener.onStageBuyCarPopupCallChooseCity();
                }
                break;
            case R.id.ll_choose_merchant:
                if(listener != null){
                    listener.onStageBuyCarPopupCallChooseMerchant();
                }
                break;
            case R.id.rtv_apply:
                if(listener != null){
                    listener.onStageBuyCarPopupApplySubmit(
                            chooseModel != null ? chooseModel.getId() : "",
                            AppCommonUtil.stringListToSplitString(chooseMerchantIds),
                            etPersonPhone.getText().toString().trim(),
                            etPersonName.getText().toString().trim(),
                            chooseCity != null ? chooseCity.getCity() : "");
                }
                break;
            case R.id.tv_agreement:
                if(listener != null){
                    listener.onStageBuyCarPopupLookAgreement();
                }
                break;
        }
    }

    public void setChooseModel(CarInfo chooseModel){
        this.chooseModel = chooseModel;
        setModelInfo();
    }
    public void setChooseCity(BaiDuLocationInfo chooseCity){
        this.chooseCity = chooseCity;
        setCityInfo();
    }
    public void setChooseMerchant(List<String> ids){
        this.chooseMerchantIds = ids;
        setMerchantInfo();
    }

    public CarInfo getChooseModel() {
        return chooseModel;
    }

    public BaiDuLocationInfo getChooseCity() {
        return chooseCity;
    }

    public List<String> getChooseMerchant() {
        return chooseMerchantIds;
    }

    public interface ICustomStageBuyCarPopupListener{
        void onStageBuyCarPopupCallChooseModel();
        void onStageBuyCarPopupCallChooseCity();
        void onStageBuyCarPopupCallChooseMerchant();
        void onStageBuyCarPopupApplySubmit(String carid, String shopids, String phone, String name, String city);
        void onStageBuyCarPopupLookAgreement();

    }

    public void releaseObj(){
        chooseModel = null;
        chooseCity = null;
        chooseMerchantIds = null;
        listener = null;
    }
}
